﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DirectoryEnumeratorAsync
{
    public static class Extensions
    {

        /// <summary>
        /// Stack trace, target site, and error message of outer and inner exception, formatted with newlines
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="exception"></param>
        /// <returns></returns>
        [DebuggerStepThroughAttribute]
        public static string VerboseExceptionString<T>(this T exception) where T : Exception
        {
            StringBuilder exceptionString = new StringBuilder();

            exceptionString.AppendLine(String.Format(" Exception: {0} Message: {1}", exception.GetType().Name, exception.Message != null ? exception.Message : "NULL"));
            exceptionString.AppendLine(String.Format(" StackTrace: {0}", exception.StackTrace != null ? exception.StackTrace : "NULL"));
            exceptionString.AppendLine(String.Format(" TargetSite: {0}", exception.TargetSite != null ? exception.TargetSite.ToString() : "NULL"));

            if (exception.InnerException != null)
            {
                exceptionString.AppendLine();
                exceptionString.AppendLine("Inner Exception:");
                exceptionString.AppendLine(exception.InnerException.VerboseExceptionString());
            }

            return exceptionString.ToString();
        }

        [DebuggerStepThroughAttribute]
        public static string CurrentMethodName()
        {
            System.Diagnostics.StackFrame frame = new System.Diagnostics.StackFrame(1);
            var method = frame.GetMethod();
            var type = method.DeclaringType;
            var name = method.Name;

            return type + "::" + name + "(): ";
        }
    }
}
